package com.zqwh.yj.sdk.utils.httpRequest.subscribers;

import android.content.Context;

import com.zqwh.yj.sdk.utils.httpRequest.progress.ProgressCancelListener;
import com.zqwh.yj.sdk.utils.httpRequest.progress.ProgressDialogHandler;

import rx.Subscriber;

/**
 * 用于在Http请求开始时，自动显示一个ProgressDialog
 * 在Http请求结束是，关闭ProgressDialog
 * 调用者自己对请求数据进行处理
 * Created by eason.yang on 2017/7/10.
 */
public class ProgressSubscriber<T> extends Subscriber<T> implements ProgressCancelListener {
    private NetCallback mNetCallback;
    private ProgressDialogHandler mProgressDialogHandler;
    private Context mContext;
    private boolean mIsPdShow = true;

    public ProgressSubscriber(NetCallback subscriberOnNextListener, Context context) {
        this.mNetCallback = subscriberOnNextListener;
        this.mContext = context;
        this.mProgressDialogHandler = new ProgressDialogHandler(context, this, true);
    }

    public ProgressSubscriber(NetCallback netCallback, Context context, boolean isPdShow) {
        this.mNetCallback = netCallback;
        this.mContext = context;
        this.mProgressDialogHandler = new ProgressDialogHandler(context, this, true);
        this.mIsPdShow = isPdShow;
    }

    private void showProgressDialog(){
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    private void dismissProgressDialog(){
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            mProgressDialogHandler = null;
        }
    }

    /**
     * 订阅开始时调用
     * 显示ProgressDialog
     */
    @Override
    public void onStart() {
        if(mIsPdShow) {
            showProgressDialog();
        }
    }

    /**
     * 完成，隐藏ProgressDialog
     */
    @Override
    public void onCompleted() {
        if(mIsPdShow) {
            dismissProgressDialog();
        }
    }

    /**
     * 对错误进行统一处理
     * 隐藏ProgressDialog
     * @param e
     */
    @Override
    public void onError(Throwable e) {
        dismissProgressDialog();
        if (mNetCallback != null) {
            mNetCallback.onError(e.getMessage());
        }
    }

    /**
     * 将onNext方法中的返回结果交给Activity或Fragment自己处理
     *
     * @param t 创建Subscriber时的泛型类型
     */
    @Override
    public void onNext(T t) {
        if (mNetCallback != null) {
            mNetCallback.onSuccess(t);
        }
    }

    /**
     * 取消ProgressDialog的时候，取消对observable的订阅，同时也取消了http请求
     */
    @Override
    public void onCancelProgress() {
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }
}